package Code.Final.StudentManagementSystem;

public class User {
    private String userName;
    private String password;
    private String identityCard;
    private String phoneNumber;



    public User() {
    }

    public User(String userName, String password, String identityCard, String phoneNumber) {
        this.userName = userName;
        this.password = setPassword(password);
        this.identityCard = identityCard;
        this.phoneNumber = phoneNumber;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        int len = userName.length();
        if (len > 2 && len < 16 && !JudgeNumber(userName) && Judge(userName)) {
            this.userName = userName;
        } else {
            System.out.println("用户名格式不满足，请重新输入");
        }
    }

    public String getPassword() {
        return password;
    }

    public String setPassword(String password) {
        this.password = password;
        return password;
    }

    public String getIdentityCard() {
        return identityCard;
    }

    public void setIdentityCard(String identityCard) {
        int len = identityCard.length();
        if (!identityCard.startsWith("0") && len == 18) {
            char c = identityCard.charAt(len - 1); //最后一位
            boolean Lens = JudgeNumber("" + c) || c == 'x' || c == 'X'; //判断最后一位可以是是数字或者字母
            if (JudgeNumber(identityCard.substring(0, 17)) && Lens) {
                this.identityCard = identityCard;
            } else {
                System.out.println("身份证格式不满足，请重新输入");
            }
        } else {
            System.out.println("身份证格式不满足，请重新输入");
        }
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        if (phoneNumber.length() == 11 && !phoneNumber.startsWith("0") &&
                JudgeNumber(phoneNumber)) {
            this.phoneNumber = phoneNumber;
        } else {
            System.out.println("电话格式不满足，请重新输入");
        }
    }

    //判断是否是数字加字母的组合
    public boolean Judge(String str) {
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }
        return true;
    }

    //判断是否是数字
    public boolean JudgeNumber(String str) {
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }
}
